﻿using Architecture.CrossCutting.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.CrossCutting.Web
{

    public class WebCustomContextStorage
    {
        private readonly IDictionary items = new Hashtable();

        public IDictionary Items
        {
            get { return items; }
        }

        internal static WebCustomContextStorage Current
        {
            get
            {
                //Check the HttpContext Current Statuts
                //It Can be null, if we are not in w web context or the thread context has been changed
                if (System.Web.HttpContext.Current == null)
                {
                    return new WebCustomContextStorage();
                }
                else
                {
                    WebCustomContextStorage context = (System.Web.HttpContext.Current.Items["Architecture.CrossCutting.Web.WebExecutionContext"] as WebCustomContextStorage);
                    if (context == null)
                    {
                        context = new WebCustomContextStorage();
                        System.Web.HttpContext.Current.Items["Architecture.CrossCutting.Web.WebExecutionContext"] = context;
                    }
                    return context;
                }

            }
        }
    }


    public class WebExecutionContext : IExecutionContext
    {

        public Guid ExecutionId
        {
            get
            {
                if (WebCustomContextStorage.Current.Items["ExecutionId"] == null)
                {
                    WebCustomContextStorage.Current.Items["ExecutionId"] = Guid.NewGuid();
                }
                return (Guid)WebCustomContextStorage.Current.Items["ExecutionId"];
            }
            set
            {
                WebCustomContextStorage.Current.Items["ExecutionId"] = value;
            }
        }

    }


}
